package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.domain.AjaxResult;
import com.itheima.common.domain.PageInfo;
import com.itheima.entity.PmsProductAttribute;
import com.itheima.entity.PmsProductAttributeCategory;
import com.itheima.dto.ProductAttributeCategoryDto;
import com.itheima.service.IPmsProductAttributeCategoryService;
import com.itheima.service.IPmsProductAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {

    @Autowired
    private IPmsProductAttributeCategoryService iPmsProductAttributeCategoryService;
    @Autowired
    private IPmsProductAttributeService iPmsProductAttributeService;

    /**
     * 添加商品属性分类
     *
     * @param pmsProductAttributeCategory
     * @return
     */
    @PostMapping("/create")
    public AjaxResult addProductAttributeCategory(@RequestBody PmsProductAttributeCategory pmsProductAttributeCategory) {
        iPmsProductAttributeCategoryService.save(pmsProductAttributeCategory);
        return AjaxResult.success("添加成功!");

    }

    /**
     * 删除单个商品属性分类
     *
     * @param id
     * @return
     */
    @GetMapping("delete/{id}")
    public AjaxResult deleteById(@PathVariable Integer id) {
        iPmsProductAttributeCategoryService.removeById(id);
        return AjaxResult.success("删除成功!");

    }

    /**
     * 分页获取所有商品属性分类
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(Integer pageNum, Integer pageSize) {
        //构造分页构造器
        Page<PmsProductAttributeCategory> productAttributePage = new Page<>(pageNum, pageSize);
        iPmsProductAttributeCategoryService.page(productAttributePage);


        PageInfo pageInfo = PageInfo.toPageList(productAttributePage);

        return AjaxResult.success("分页查询成功", pageInfo);
    }

    /**
     * 获取所有商品属性分类及其下属性
     *
     * @return
     */
    @GetMapping("/list/withAttr")
    public AjaxResult getAllProductAttribute() {
        //获取到PmsProductAttributeCategory所有
        List<PmsProductAttributeCategory> productAttributeCategoryList = iPmsProductAttributeCategoryService.list();
        //构造条件构造器
        LambdaQueryWrapper<PmsProductAttribute> pmsProductAttributeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //构造返回值对象
        ProductAttributeCategoryDto productAttributeCategoryDto = new ProductAttributeCategoryDto();
        for (PmsProductAttributeCategory pmsProductAttributeCategory : productAttributeCategoryList) {
            pmsProductAttributeLambdaQueryWrapper.eq(PmsProductAttribute::getProductAttributeCategoryId, pmsProductAttributeCategory.getId());
            //查询属性id的所有
            List<PmsProductAttribute> list = iPmsProductAttributeService.list(pmsProductAttributeLambdaQueryWrapper);
            //赋值
            productAttributeCategoryDto.setAttributeCount(pmsProductAttributeCategory.getAttributeCount());
            productAttributeCategoryDto.setId(pmsProductAttributeCategory.getId());
            productAttributeCategoryDto.setName(pmsProductAttributeCategory.getName());
            productAttributeCategoryDto.setParamCount(pmsProductAttributeCategory.getParamCount());
            productAttributeCategoryDto.setList(list);
        }
        return AjaxResult.success("查询成功", productAttributeCategoryDto);
    }

}
